package com.ruoyi.project.system.news.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.controller.ProfileController;
import com.ruoyi.project.system.user.domain.User;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.news.domain.News;
import com.ruoyi.project.system.news.service.INewsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 校园新闻 信息操作处理
 * 
 * @author Waynnn
 * @date 2019-04-04
 */
@Controller
@RequestMapping(value = "/system/news")
public class NewsController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(ProfileController.class);
	public static final String UPLOAD_PATH = "/profile/upload/";
    private String prefix = "system/news";
	
	@Autowired
	private INewsService newsService;

	@Autowired
	private IDeptService deptService;

	@Autowired
	private IRoleService roleService;


	@RequiresPermissions("system:news:view")
	@GetMapping()
	public String news(ModelMap mmap)
	{
		User user=getSysUser();
		List<Role> roles=roleService.selectRolesByUserIdForNews(user.getUserId());
		for (Role role:roles){
			if (role.getRoleId()==1L){
				return prefix + "/newsForAdmin";
			}
		}
		mmap.put("school",deptService.findSchoolByDeptId(user.getDeptId()));
	    return prefix + "/news";
	}
	
	/**
	 * 查询校园新闻列表
	 */
	@RequiresPermissions("system:news:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(News news)
	{
		User user=getSysUser();
		List<Role> roles=roleService.selectRolesByUserId(user.getUserId());
		for (Role role:roles){
			if (role.getRoleName().equals("管理员")){
				startPage();
				List<News> newsForAdmin=newsService.selectNewsList(news);
				return getDataTable(newsForAdmin);
			}
		}
		Dept dept=deptService.findSchoolObjByDeptId(user.getDeptId());
		startPage();
        List<News> list = newsService.selectNewsListByDeptId(dept.getDeptId());
		return getDataTable(list);
	}

	
	/**
	 * 新增校园新闻
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		News news=new News();
		news.setImagePath(null);
		mmap.put("news",news);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存校园新闻
	 */
	@RequiresPermissions("system:news:add")
	@Log(title = "校园新闻", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(@RequestParam("image")MultipartFile file,News news)
	{
		try {
			String filePath = RuoYiConfig.getUploadPath();
			// 上传并返回新文件名称
			String fileName = FileUploadUtils.upload(filePath, file);
			news.setImagePath(fileName);
			return toAjax(newsService.insertNews(news));
		}catch (Exception e){
			return AjaxResult.error(e.getMessage());
		}

	}

	/**
	 * 修改校园新闻
	 */
	@GetMapping("/edit/{newsId}")
	public String edit(@PathVariable("newsId") Long newsId, ModelMap mmap)
	{
		News news = newsService.selectNewsById(newsId);
		mmap.put("news", news);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存校园新闻
	 */
	@RequiresPermissions("system:news:edit")
	@Log(title = "校园新闻", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(@RequestParam("image")MultipartFile file,News news)
	{
		try {
			if (!file.isEmpty())
			{
				String filePath = RuoYiConfig.getUploadPath();
				// 上传并返回新文件名称
				String fileName = FileUploadUtils.upload(filePath, file);
				news.setImagePath(fileName);
				return toAjax(newsService.updateNews(news));
			}
			return error();
		}catch (Exception e){
			return AjaxResult.error(e.getMessage());
		}
	}
	
	/**
	 * 删除校园新闻
	 */
	@RequiresPermissions("system:news:remove")
	@Log(title = "校园新闻", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(newsService.deleteNewsByIds(ids));
	}


}
